<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="700px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="150px">
          <el-form-item label="商家及对接人" prop="sponsorTeamId">
            <el-input
              v-if="false"
              v-model="form.sponsorTeamId"
              style="width: 370px;"
            />
            <div v-if="form.sponsorTeamId">
              {{ dialogTableDataSponsorTeamId.showInputValue }}
            </div>
            <!-- 新增组件弹出式表单 -->
            <dialogTable
              :dialogTableData.sync="dialogTableDataSponsorTeamId"
              @select-callback="dialogTableSelectedCallBackSponsorTeamId"
            ></dialogTable>
          </el-form-item>

          <el-form-item label="套餐名称" prop="benefitName">
            <el-input v-model="form.benefitName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="套餐介绍" prop="benefitDetail">
            <el-input v-model="form.benefitDetail" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="原价" prop="benefitPriceOri">
            <el-input v-model="form.benefitPriceOri" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="现价" prop="benefitPriceCur">
            <el-input v-model="form.benefitPriceCur" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="成本" prop="benefitPriceCost">
            <el-input v-model="form.benefitPriceCost" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="主图" prop="benefitPicMain">
              <MaterialList
                      v-model="benefitPicMainArr"
                      style="width: 915px;"
                      type="image"
                      :num="1"
                      :width="150"
                      :height="150"
              />
          </el-form-item>
          <el-form-item label="详情图" prop="benefitPicDetail">
              <MaterialList
                      v-model="benefitPicDetailArr"
                      style="width: 915px;"
                      type="image"
                      :num="1"
                      :width="150"
                      :height="150"
              />
          </el-form-item>
          <el-form-item label="库存">
            <el-input v-model="form.inventory" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="有效期从">
            <el-date-picker v-model="form.startDate" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="有效期至">
            <el-date-picker v-model="form.endDate" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="状态" prop="benefitStatus">
            <el-select v-model="form.benefitStatus" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.SPONSOR_BENEFIT_STATUS"
                :key="item.id"
                :label="item.label"
                :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="备注说明">
            <el-input v-model="form.remark" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns.visible('sponsorName')" prop="sponsorName" label="商家" />
        <el-table-column v-if="columns.visible('sponsorTypeMeaning')" prop="sponsorTypeMeaning" label="商家" />
        <el-table-column v-if="columns.visible('sponsorTeamName')" prop="sponsorTeamName" label="对接人" />
        <el-table-column v-if="columns.visible('sponsorTeamPhone')" prop="sponsorTeamPhone" label="电话" />
        <el-table-column v-if="columns.visible('benefitName')" prop="benefitName" label="套餐名称" />
        <el-table-column v-if="columns.visible('benefitStatus')" prop="benefitStatus" label="状态">
          <template slot-scope="scope">
            {{ dict.label.SPONSOR_BENEFIT_STATUS[scope.row.benefitStatus] }}
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('inventory')" prop="inventory" label="库存" />
        <el-table-column v-if="columns.visible('benefitDetail')" prop="benefitDetail" label="套餐介绍" />
        <el-table-column v-if="columns.visible('startDate')" prop="startDate" label="有效期从">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.startDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('endDate')" prop="endDate" label="有效期至">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.endDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('benefitPriceOri')" prop="benefitPriceOri" label="原价" />
        <el-table-column v-if="columns.visible('benefitPriceCur')" prop="benefitPriceCur" label="现价" />
        <el-table-column v-if="columns.visible('benefitPriceCost')" prop="benefitPriceCost" label="成本" />
          <el-table-column v-if="columns.visible('benefitPicMain')" prop="benefitPicMain" label="主图">
            <template slot-scope="scope">
              <a :href="scope.row.benefitPicMain" style="color: #42b983" target="_blank">
                <img :src="scope.row.benefitPicMain" alt="点击打开" class="table-img" />
              </a>
            </template>
          </el-table-column>
          <el-table-column v-if="columns.visible('benefitPicDetail')" prop="benefitPicDetail" label="详情图">
            <template slot-scope="scope">
              <a :href="scope.row.benefitPicDetail" style="color: #42b983" target="_blank">
                <img :src="scope.row.benefitPicDetail" alt="点击打开" class="table-img" />
              </a>
            </template>
          </el-table-column>

        <el-table-column v-if="columns.visible('remark')" prop="remark" label="备注说明" />

        <el-table-column v-permission="['admin','tanzerSponsorBenefit:edit','tanzerSponsorBenefit:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudTanzerSponsorBenefit from '@/api/tanzer/tanzerSponsorBenefit'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import MaterialList from "@/components/material";
import dialogTable from "@/views/components/dialogTable/index.vue";

// crud交由presenter持有
const defaultCrud = CRUD({ title: '商家套餐', url: 'api/tanzerSponsorBenefit', sort: 'id,desc', crudMethod: { ...crudTanzerSponsorBenefit }})
const defaultForm = { id: null, sponsorId: null, sponsorTeamId: null, benefitName: null, benefitDetail: null, benefitPriceOri: null, benefitPriceCur: null, benefitPriceCost: null, benefitPicMain: null, benefitPicDetail: null, inventory: null, startDate: null, endDate: null, benefitStatus: null, remark: null, isDel: null, createTime: null, createBy: null, updateBy: null, updateTime: null }
export default {
  name: 'TanzerSponsorBenefit',
  components: {dialogTable, pagination, crudOperation, rrOperation, udOperation ,MaterialList},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  dicts: ['SPONSOR_BENEFIT_STATUS'],
  data() {
    return {
      benefitPicMainArr:[],benefitPicDetailArr:[],
      permission: {
        add: ['admin', 'tanzerSponsorBenefit:add'],
        edit: ['admin', 'tanzerSponsorBenefit:edit'],
        del: ['admin', 'tanzerSponsorBenefit:del']
      },
      rules: {
        sponsorId: [
          { required: true, message: '商家不能为空', trigger: 'blur' }
        ],
        sponsorTeamId: [
          { required: true, message: '商家对接人不能为空', trigger: 'blur' }
        ],
        benefitName: [
          { required: true, message: '套餐名称不能为空', trigger: 'blur' }
        ],
        benefitDetail: [
          { required: true, message: '套餐介绍不能为空', trigger: 'blur' }
        ],
        benefitPriceOri: [
          { required: true, message: '原价不能为空', trigger: 'blur' }
        ],
        benefitPriceCur: [
          { required: true, message: '现价不能为空', trigger: 'blur' }
        ],
        benefitPriceCost: [
          { required: true, message: '成本不能为空', trigger: 'blur' }
        ],
        benefitPicMain: [
          { required: true, message: '主图不能为空', trigger: 'blur' }
        ],
        benefitPicDetail: [
          { required: true, message: '详情图不能为空', trigger: 'blur' }
        ],
        benefitStatus: [
          { required: true, message: '状态不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'sponsorId', display_name: '商家' },
        { key: 'sponsorTeamId', display_name: '商家对接人' },
        { key: 'benefitName', display_name: '套餐名称' },
        { key: 'benefitPriceCur', display_name: '现价' },
        { key: 'benefitStatus', display_name: '状态' }
      ],

      //自定义lov数据源
      dialogTableDataSponsorTeamId: {
        title: "商家信息",
        dialogVisible: false,
        url: "api/tanzerSponsorTeam",
        sort: "id,desc",
        buttonContext: "选择商家及对接人",
        showInputValue: "",
        queryTypeOptions: [
          { key: 'sponsorName', display_name: '商家' },
          { key: 'sponsorTeamName', display_name: '对接人' },
          { key: 'sponsorTeamPhone', display_name: '对接人电话' },
        ],
        elTtableColumns: [
          {
            columnProp: "sponsorName",
            columnLabel: "名称"
          },
          {
            columnProp: "sponsorCode",
            columnLabel: "编码"
          },
          {
            columnProp: "sponsorTypeMeaning",
            columnLabel: "类型"
          },
          {
            columnProp: "sponsorTeamName",
            columnLabel: "对接人"
          },
          {
            columnProp: "sponsorTeamPhone",
            columnLabel: "电话"
          }
        ]
      },

    }
  },
  watch: {
    "benefitPicMainArr":function(val) {
      if (val) {
        this.form.benefitPicMain = val.join(",");
      }
    },
    "benefitPicDetailArr":function(val) {
      if (val) {
        this.form.benefitPicDetail = val.join(",");
      }
    },
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      }else{
        delete this.crud.params.sponsorId
        delete this.crud.params.sponsorTeamId
        delete this.crud.params.benefitName
        delete this.crud.params.benefitPriceCur
        delete this.crud.params.benefitStatus
      }
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      if (form.benefitPicMain && form.id) {
        this.benefitPicMainArr = form.benefitPicMain.split(",");
      }else{
        this.benefitPicMainArr=[]
      }
      if (form.benefitPicDetail && form.id) {
        this.benefitPicDetailArr = form.benefitPicDetail.split(",");
      }else{
        this.benefitPicDetailArr=[]
      }
    },

    [CRUD.HOOK.afterToEdit](crud, form) {

      this.setInputValue(
        this.dialogTableDataSponsorTeamId,
        "showInputValue",
        form,
        "sponsorTeamId",
        "content",
        0,
        "sponsorName",
        "api/tanzerSponsorTeam",
        {
          page: 0,
          size: 10,
          sort: "id,desc",
          id: form.sponsorTeamId
        }
      );
    },

    //省市区数据源
    dialogTableSelectedCallBackSponsorTeamId(row) {
      this.form.sponsorTeamId = row.id;
      if (this.form.sponsorTeamId) {
        this.dialogTableDataSponsorTeamId.showInputValue = row.sponsorName+'-'+row.sponsorTeamName;
      }
    },
  }
}



</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
</style>
